<?php

/**
 * 后台 广告位 模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: link.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class adcatAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('adcat');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_user');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getAdcatList();
        $this->V('adcat');
    }

    /**
     * 新增/修改广告位
     */
    public function add()
    {
        $ad_cid = intval($this->getParam('ad_cid'));
        if ($ad_cid > 0) {
            $editinfo = $this->tmp_model->getAdcatInfo($ad_cid);
        } else {
            $editinfo = array(
                'ad_cid' => 0,
                'ad_cname' => '',
                'ad_corder' => '',
                'ad_ctime' => ''
            );
        }
                
        $this->assign('editinfo', $editinfo);
        $this->V('adcat');
    }

    /**
     * 新增/修改广告位页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 3) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $ad_cid = intval($this->getParam('ad_cid'));
        $ad_cname = $this->H($this->getParam('ad_cname'));
        $ad_corder = intval($this->getParam('ad_corder'));
        $ad_ctime = time();
        

        /* 验证不能为空 */
        if (empty($ad_cname)) {
            $this->redirect('广告栏目不能为空!');
            exit;
        }

        // TODO goon to verify
        $ad_corder = empty($ad_corder) ? 0 : $ad_corder;
        $info = array(
            'ad_cname' => $ad_cname,
            'ad_corder' => $ad_corder,
            'ad_ctime' => $ad_ctime                  
        );

        if ($ad_cid > 0) {
            $adcat_page = HttpResponse::getCookie('adcat_page');
            //update save article
            $where = 'ad_cid = ' . $ad_cid . '';
            $rs = $this->tmp_model->addAdcat($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改广告位成功', PHP_SELF . '?m=adcat&page= ' . $adcat_page . '');
            } else {
                $this->redirect('修改广告位失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addAdcat($info);
            if ($rs) {
                $this->redirect('添加广告位成功', PHP_SELF . '?m=adcat');
            } else {
                $this->redirect('添加广告位失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = intval($this->getParam('ad_cid'));
        $id_a = !empty($_POST['id_a']) ? $_POST['id_a'] : '';
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delAdcat($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $adcat_page = HttpResponse::getCookie('adcat_page');
            $this->redirect('删除广告位成功', PHP_SELF . '?m=adcat&page=' . $adcat_page . '');
        } else {
            $this->redirect('删除广告位失败，请重试！');
        }
    }

}